package kr.co.wikibook.usermng;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AddUserActivity extends Activity {
	
	private Button btDelete, btSave;
	private EditText etName, etAge, etArea;
	private String name, age, area;
	private final String TABLE = "user_info";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.input_form);
		
		etName = (EditText)findViewById(R.id.etName);
		etAge = (EditText)findViewById(R.id.etAge);
		etArea = (EditText)findViewById(R.id.etArea);
		
		btDelete = (Button)findViewById(R.id.deleteBt);
		btDelete.setVisibility(View.GONE);
		btSave = (Button)findViewById(R.id.saveBt);
		btSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				SQLiteDatabase db = null;
				if(db == null){
					db = openOrCreateDatabase("sqlite_test.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
				}				
				
				name = etName.getText().toString();
				age = etAge.getText().toString();
				area = etArea.getText().toString();
				
				ContentValues values = new ContentValues();
				values.put("name", name);
				values.put("age", age);
				values.put("area", area);
				long aid = db.insert(TABLE, null, values);
				
				if(aid == -1){
					Log.e(getLocalClassName(),"db insert - error occurred");
				}
				if(db != null){
					db.close();
				}
				finish();
				
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

}
